//
//  prox.m
//  prototipo1Movies
//
//  Created by mayraDonaji on 9/25/10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "prox.h"
#import "busqueda.h"
#import "prototipo1MoviesViewController.h"
#import "recomenda.h"
#import "noticias.h"
#import "Movie.h"
#import "help.h"


@implementation prox

@synthesize top, search, noticias, inicio, help,  input, scrollView, pageControl, pageControlIsChangingPage;

/*
 // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
        // Custom initialization
    }
    return self;
}
*/


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	[self setupPage];

}


/*
// Override to allow orientations other than the default portrait orientation.
 
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/


/*
void MyCreateAndDrawBitmapImage (CGContextRef myContext, CGRect myContextRect, const char *filename)
{	
	
	CGImageRef image;
	CGDataProviderRef provider;
	CFStringRef path;
	CFURLRef url;
	
	path = CFStringCreateWithCString (NULL, filename,kCFStringEncodingUTF8);
	url = CFURLCreateWithFileSystemPath (NULL, path,kCFURLPOSIXPathStyle, NO);
	
	CFRelease(path);
	
	provider = CGDataProviderCreateWithURL (url);
	
	CFRelease (url);
	
	image = CGImageCreateWithJPEGDataProvider (provider,NULL,true,kCGRenderingIntentDefault);
	
	CGDataProviderRelease (provider);// 5
	
	CGContextDrawImage (myContext, myContextRect, image);
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
	
	[UIView beginAnimations:@"MoveAndStrech" context:nil];
	[UIView setAnimationDuration:1];
	[UIView setAnimationBeginsFromCurrentState:YES];
	
	if([touches count] == 1) {
		UITouch *touch = [touches anyObject];
		//bajar.center = [touch locationInView:self];
	}
	
	
	[UIView commitAnimations];
	
}
*/	


-(IBAction) top :(id)sender{
	recomenda * recomiendas = [[recomenda alloc]initWithNibName: nil bundle:nil];
	recomiendas.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
	recomiendas.input = self.input;
	[self presentModalViewController:recomiendas animated:YES];	
}

-(IBAction) search :(id)sender{
	busqueda * busquedas = [[busqueda alloc]initWithNibName: nil bundle:nil];
	busquedas.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;	
	busquedas.input = self.input;
	[self presentModalViewController:busquedas animated:YES];	
}

-(IBAction) noticias :(id)sender{
	noticias * noticia = [[noticias alloc]initWithNibName: nil bundle:nil];
	noticia.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
	noticia.input = self.input;
	[self presentModalViewController:noticia animated:YES];	
}

-(IBAction) inicio :(id)sender{
	prototipo1MoviesViewController * inicios = [[prototipo1MoviesViewController alloc]initWithNibName: nil bundle:nil];
	inicios.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;	
	inicios.mDB = self.input;
	[self presentModalViewController:inicios animated:YES];	
}

-(IBAction) help :(id)sender{
	help * helps = [[help alloc]initWithNibName: nil bundle:nil];
	helps.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
	[self presentModalViewController:helps animated:YES];
}

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}

- (void)setupPage
{
	
	scrollView.delegate = self;
	
	//[self.scrollView setBackgroundColor:[UIColor blackColor]];
	[scrollView setCanCancelContentTouches:NO];
	
	scrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
	scrollView.clipsToBounds = YES;
	scrollView.scrollEnabled = YES;
	scrollView.pagingEnabled = YES;
	
	NSMutableArray *topMovies = input.estrenos;
	NSInteger size = [topMovies count];
	NSLog(@"%i", size);
	CGFloat cx = 0;
	for (NSUInteger i = 0, max = [topMovies count]; i < max; i++) {
		Movie *thisMovie = [topMovies objectAtIndex:i];
		
		// Getting the URL of the image
		NSString *urlString = thisMovie.image;
		
		// Creating the rectangle for placing the image
		CGRect rect = CGRectMake(10+cx, 0, scrollView.frame.size.width * 0.43, scrollView.frame.size.height*0.6);
		
		
		// - Showing Images
		NSString *embedHTML = @"\
		<html><head>\
		<style type=\"text/css\">\
		body {\
		background-color: transparent;\
		color: white;\
		}\
		</style>\
		</head><body style=\"margin:0\">\
		<img src=\"%@\" width=\"%0.0f\" height=\"%0.0f\"/>\
		</body></html>";
		
		NSString *html = [NSString stringWithFormat:embedHTML, urlString, rect.size.width, rect.size.height];
		// - Creating the view of the image
		UIWebView *videoView = [[UIWebView alloc] initWithFrame:rect];
		[videoView loadHTMLString:html baseURL:nil];
		[scrollView addSubview:videoView];
		[videoView release];
		
		// - Creating the title label
		CGRect titleRect = CGRectMake(scrollView.frame.size.width * 0.43+30+cx, 0, scrollView.frame.size.width * 0.45, scrollView.frame.size.height*0.6); 
		UILabel * titleLabel = [[UILabel alloc] initWithFrame:titleRect];
		titleLabel.text = thisMovie.title;
		titleLabel.textColor = [UIColor blackColor];
		titleLabel.backgroundColor = [UIColor clearColor];
		titleLabel.lineBreakMode = UILineBreakModeWordWrap;
		titleLabel.numberOfLines = 0;
		[scrollView addSubview:titleLabel];
		[titleLabel release];
		
		// - Creating synopsis label
		CGRect synopsisRect = CGRectMake(cx + scrollView.frame.size.width * 0.05, scrollView.frame.size.height*0.63, scrollView.frame.size.width * 0.9, scrollView.frame.size.height*0.3); 
		UILabel * synopsisLabel = [[UILabel alloc] initWithFrame:synopsisRect];
		synopsisLabel.text = thisMovie.synopsis;
		synopsisLabel.textColor = [UIColor blackColor];
		synopsisLabel.font = [UIFont fontWithName:@"Arial" size:(13.0)];
		synopsisLabel.backgroundColor = [UIColor clearColor];
		synopsisLabel.lineBreakMode = UILineBreakModeWordWrap;
		synopsisLabel.numberOfLines = 0;
		[scrollView addSubview:synopsisLabel];
		[synopsisLabel release];
		
		
		cx += scrollView.frame.size.width;
	}
	
	/*
	 // -- Original implementation --
	 NSUInteger nimages = 0;
	 CGFloat cx = 0;
	 for (; ; nimages++) {
	 
	 // - Images in Scroll View -
	 NSString *imageName = [NSString stringWithFormat:@"image%d.jpg", (nimages + 1)];
	 UIImage *image = [UIImage imageNamed:imageName];
	 if (image == nil) {
	 break;
	 }
	 UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
	 
	 CGRect rect = imageView.frame;
	 rect.size.height = image.size.height;
	 rect.size.width = image.size.width;
	 rect.origin.x = ((scrollView.frame.size.width - image.size.width) / 2) + cx;
	 rect.origin.y = ((scrollView.frame.size.height - image.size.height) / 2);
	 
	 imageView.frame = rect;
	 
	 
	 //[scrollView addSubview:imageView];
	 //[self embedYouTube:@"http://www.youtube.com/watch?v=WL2l_Q1AR_Q" frame:CGRectMake(20, 20, 100, 100)];
	 
	 //NSString *urlString = @"http://www.youtube.com/v/WL2l_Q1AR_Q";
	 NSString *urlString = @"http://codeit.com.mx/images/integrantes/david.jpg";
	 
	 
	 // -For showing YouTube Videos
	 NSString *embedHTML = @"\
	 <html><head>\
	 <style type=\"text/css\">\
	 body {\
	 background-color: transparent;\
	 color: white;\
	 }\
	 </style>\
	 </head><body style=\"margin:0\">\
	 <embed id=\"yt\" src=\"%@\" type=\"application/x-shockwave-flash\" \
	 width=\"%0.0f\" height=\"%0.0f\"></embed>\
	 </body></html>";
	 
	 
	 // - Showing Images
	 NSString *embedHTML = @"\
	 <html><head>\
	 <style type=\"text/css\">\
	 body {\
	 background-color: transparent;\
	 color: white;\
	 }\
	 </style>\
	 </head><body style=\"margin:0\">\
	 <img src=\"%@\" width=\"%0.0f\" height=\"%0.0f\"/>\
	 </body></html>";
	 
	 NSString *html = [NSString stringWithFormat:embedHTML, urlString, rect.size.width, rect.size.height];
	 UIWebView *videoView = [[UIWebView alloc] initWithFrame:rect];
	 [videoView loadHTMLString:html baseURL:nil];
	 [scrollView addSubview:videoView];
	 [videoView release];
	 
	 [imageView release];
	 
	 cx += scrollView.frame.size.width;
	 }
	 */
	
	self.pageControl.numberOfPages = [topMovies count];
	[scrollView setContentSize:CGSizeMake(cx, [scrollView bounds].size.height)];
}

- (void)scrollViewDidScroll:(UIScrollView *)_scrollView
{
    if (pageControlIsChangingPage) {
        return;
    }
	
	/*
	 *	We switch page at 50% across
	 */
    CGFloat pageWidth = _scrollView.frame.size.width;
    int page = floor((_scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
    pageControl.currentPage = page;
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)_scrollView 
{
    pageControlIsChangingPage = NO;
}

#pragma mark -
#pragma mark PageControl stuff
- (IBAction)changePage:(id)sender 
{
	/*
	 *	Change the scroll view
	 */
    CGRect frame = scrollView.frame;
    frame.origin.x = frame.size.width * pageControl.currentPage;
    frame.origin.y = 0;
	
    [scrollView scrollRectToVisible:frame animated:YES];
	
	/*
	 *	When the animated scrolling finishings, scrollViewDidEndDecelerating will turn this off
	 */
    pageControlIsChangingPage = YES;
}

- (void)embedYouTube:(NSString *)urlString frame:(CGRect)frame {
	NSString *embedHTML = @"\
    <html><head>\
	<style type=\"text/css\">\
	body {\
	background-color: transparent;\
	color: white;\
	}\
	</style>\
	</head><body style=\"margin:0\">\
    <embed id=\"yt\" src=\"%@\" type=\"application/x-shockwave-flash\" \
	width=\"%0.0f\" height=\"%0.0f\"></embed>\
    </body></html>";
	NSString *html = [NSString stringWithFormat:embedHTML, urlString, frame.size.width, frame.size.height];
	UIWebView *videoView = [[UIWebView alloc] initWithFrame:frame];
	[videoView loadHTMLString:html baseURL:nil];
	[scrollView addSubview:videoView];
	[videoView release];
}


@end
